package com.ln.adoms.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ln.adoms.entity.Order;
import com.ln.adoms.entity.neum.Status;
import com.ln.adoms.service.OrderService;
import com.ln.core.orm.Filtration;
import com.ln.core.orm.Filtration.MatchType;
import com.ln.core.orm.PageData;
import com.ln.core.security.entity.Users;
import com.ln.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/order")
public class OrderAction extends BaseAction {

	private static final Logger LOGGER = Logger.getLogger(GoodsAction.class);

	@Resource
	private OrderService orderService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<Order> pageData = new PageData<Order>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData.getFiltrations().add(new Filtration(MatchType.EQ, true, "type"));
		pageData = orderService.find(pageData);
		return new ModelMap(pageData);
	}

	@RequestMapping("/myOrderList")
	public ModelMap myOrderList(HttpServletRequest request) {
		PageData<Order> pageData = new PageData<Order>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Filtration filter = new Filtration(MatchType.EQ, getCurrentUser(request).getDepartment().getId(), "department.id");
		Filtration ftype = new Filtration(MatchType.EQ, true, "type");
		pageData.getFiltrations().add(filter);
		pageData.getFiltrations().add(ftype);
		pageData = orderService.find(pageData);
		return new ModelMap(pageData);
	}

	@RequestMapping("/dInStock")
	public ModelMap dInStock(HttpServletRequest request) {
		PageData<Order> pageData = new PageData<Order>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Filtration filter = new Filtration(MatchType.EQ, getCurrentUser(request).getDepartment().getId(), "department.id");
		Filtration fStatus = new Filtration(MatchType.EQ, Status.ORDER_HAS_SHIPED, "status");
		pageData.getFiltrations().add(filter);
		pageData.getFiltrations().add(fStatus);
		pageData = orderService.find(pageData);
		return new ModelMap(pageData);
	}

	@RequestMapping("/dealOrderList")
	public ModelMap dealOrderList(HttpServletRequest request) {
		PageData<Order> pageData = new PageData<Order>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Filtration filter = new Filtration(MatchType.EQ, getCurrentUser(request).getDepartment().getId(), "departmentTarget.id");
		Filtration ftype = new Filtration(MatchType.EQ, true, "type");
		pageData.getFiltrations().add(filter);
		pageData.getFiltrations().add(ftype);
		pageData = orderService.find(pageData);
		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/deal/{id}", method = RequestMethod.GET)
	public String deal(@PathVariable("id") Integer id, Model model) {
		Order order = orderService.find(id);
		model.addAttribute(order);
		return "/adoms/order/deal";
	}

	@RequestMapping(value = "/deal", method = RequestMethod.POST)
	public String deal(HttpServletRequest request, Model model, Order order) {
		orderService.deal(this.getCurrentUser(request), order);
		return "redirect:/adoms/order/dealOrderList";// 重定向
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		return "/adoms/order/input";
	}

	@RequestMapping(value = "/myNew")
	public String myNew(Model model) {
		return "/adoms/order/input";
	}

	@RequestMapping(value = "/order", method = RequestMethod.GET)
	public String order(Model model, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		orderService.commonOrder(model, departmentId);
		return "/adoms/order/order";
	}

	@RequestMapping(value = "/order", method = RequestMethod.POST)
	public String order(Model model, Order order, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		Users user = this.getCurrentUser(request);
		orderService.order(model, order, user, request, departmentId);
		return "/adoms/order/order";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		Order order = orderService.find(id);
		model.addAttribute(order);
		return "/adoms/order/input";
	}

	@RequestMapping(value = "/myEdit/{id}")
	public String myEdit(@PathVariable("id") Integer id, Model model) {
		Order order = orderService.find(id);
		model.addAttribute(order);
		return "/adoms/order/myInput";
	}

	@RequestMapping(value = "/confirm/{id}")
	public String confirm(@PathVariable("id") Integer id, Model model) {
		Order order = orderService.find(id);
		model.addAttribute("order",order);
		return "/adoms/order/confirm";
	}

	@RequestMapping(value = "/mySave", method = RequestMethod.POST)
	public String mySave(HttpServletRequest request, Order order) {
		boolean isReceiptOrder;
		try {
			isReceiptOrder = orderService.saveOrder(this.getCurrentUser(request), order);
			if (isReceiptOrder) {
				return "redirect:/adoms/goods/orderGoods?orderId=" + order.getId();// 重定向
			}
		} catch (Exception e) {
			LOGGER.error(e.getCause());
		}
		return "redirect:/adoms/order/myOrderList";// 重定向
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Order order) {
		if (order.isNew()) {
			orderService.save(order);
		} else {
			orderService.update(order);

		}
		return "redirect:/adoms/order/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		orderService.delete(id);
		return "redirect:/adoms/order/list";// 重定向
	}

	@RequestMapping(value = "/myDelete/{id}")
	public String myDelete(@PathVariable("id") Integer id) {
		orderService.delete(id);
		return "redirect:/adoms/order/myOrderList";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		orderService.visible(id);
		return "redirect:/adoms/order/list";// 重定向
	}

	@RequestMapping(value = "/view/{id}")
	public String viewOrder(@PathVariable("id") Integer id, Model model) {
		Order order = orderService.find(id);
		model.addAttribute(order);
		return "/adoms/order/view";
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		orderService.unVisible(id);
		return "redirect:/adoms/order/list";// 重定向
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), false));
	}

}
